<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar van-nav-bar_title" fixed>
      <van-button class="search-btn" icon="search" round slot="title" to="/search" type="small">搜索</van-button>
    </van-nav-bar>

    <!-- 标签页 -->
    <van-tabs animated class="channel-tabs" swipeable v-model="active">
      <van-tab :key="item.id" :title="item.name" v-for="item in channels">
        <keep-alive>
          <article-list :channel="item"></article-list>
        </keep-alive>
      </van-tab>

      <div class="zwf" slot="nav-right"></div>

      <div @click="isPopupShow=true" class="hambuger-btn" slot="nav-right">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <!-- 弹出层 -->
    <van-popup
      :style="{ height: '100%' }"
      close-icon-position="top-left"
      closeable
      position="bottom"
      v-model="isPopupShow"
    >
      <channel-edit :active="active" :my-channels="channels" @update-active="onUpdateActive"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannnels } from '@/api/user'
import ArticleList from './components/ArticleList.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '../../utils/storage'



export default {
  components: { ArticleList, ChannelEdit },
  name: 'Home',
  data () {
    return {
      active: 0,
      channels: [],
      isPopupShow: false
    }
  },
  computed: {
    // ...mapState(['user'])
    user () {
      return this.$store.state.user
    }
  },
  created () {
    this.loadChannels()
  },
  methods: {
    async loadChannels () {
      try {
        // const { data: res } = await getUserChannnels()
        // console.log(res)
        let channels = []
        if (this.user) {
          const { data: res } = await getUserChannnels()
          channels = res.data.channels
        } else {
          const localChannels = getItem('TOUTIAO_CHANNELS')
          if (localChannels) {
            channels = localChannels
          } else {
            const { data: res } = await getUserChannnels()
            channels = res.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取频到列表失败')
      }
    },
    onUpdateActive (index, isPopupShow = true) {
      this.active = index
      this.isPopupShow = isPopupShow
    }

  }
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      color: #fff;
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tab {
      width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      left: 0;
      right: 0;
      z-index: 1;
      height: 82px;
    }
    .van-tabs__line {
      width: 31px;
      height: 6px;
      bottom: 8px;
      background-color: #3296fa;
    }
    .zwf {
      width: 66px;
      flex-shrink: 0;
    }
    .hambuger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      .toutiao {
        font-size: 33px;
      }
      &:before {
        content: "";
        width: 2px;
        height: 100%;
        position: absolute;
        left: 0;
        background-image: url("../../assets/gradient-gray-line.png");
        background-size: contain;
      }
    }
  }
}
</style>